// development_deck.cc
#include "development_deck.h"


const int DevelopmentDeck::kMaxNumPerType[DevelopmentDeck::kNumDevTypes] = {
  14,  // 14 soldiers
  2,   // 2 monopoly cards
  2,   // 2 year of plenty cards
  2,   // 2 road building cards
  5,   // 5 victory points 
  0,   // 0 invalid cards
};

static const string development_card_name[DevelopmentDeck::kNumDevTypes] = {
  "soldier",
  "monopoly",
  "year of plenty",
  "road building",
  "victory point",
  "invalid development card",
};

const string &DevelopmentDeck::CardName(int type) {
  return development_card_name[type];
}

int DevelopmentDeck::NumCardsOfType(int type) {
  assert(type >=0 && type < kNumDevTypes);
  return kMaxNumPerType[type];
}

DevelopmentDeck::DevelopmentDeck() {
  //Seed the random number generator.
  srand(static_cast<int>(time(0)));
  Reset();
}

void DevelopmentDeck::Reset() {
  size_ = 0;
  for (int i = 0; i < DevelopmentDeck::kNumDevTypes; i++) {
    counts_[i] = kMaxNumPerType[i];
    size_ += kMaxNumPerType[i];
  }
}

void DevelopmentDeck::PrintDeck() {
  printf("Development Deck State:\n");
  for (int i = 0; i < DevelopmentDeck::kNumDevTypes; i++) {
    printf("\t%s: %d\n", CardName(i).c_str(), counts_[i]);
  }
}

int DevelopmentDeck::GetCard() {
  if (size_ == 0)
    return INVALID;
  int type = PickAType();
  if (type != INVALID) {
    counts_[type]--;
    size_--;
  }
  return type;
}

bool DevelopmentDeck::IsValid(int type){
  return ( type >= 0 && type < INVALID);
}

int DevelopmentDeck::PickAType() {
  int random_int = (rand() % size_);
  int total = 0;
  for (int i = 0; i < DevelopmentDeck::kNumDevTypes; i++) {
    total += counts_[i];
    if (random_int < total)
      return i;
  }
  return INVALID;
}


